//
//  LGReportDetailHospitalController2.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/2.
//  出院病情证明书2

#import "LGReportDetailHospitalController2.h"
#import "LGGetWebViewController.h"
#import "LGReportDetailHeaderView.h"
//诊疗小结
#import "LGReportDetailDiagnosisCell.h"
//辅助检查-描述
#import "LGReportDetailInspectDescCell.h"
//辅助检查-指标
#import "LGReportDetailInspectTargetCell.h"
//照护计划
#import "LGReportDetailCarePlanCell.h"
//用药方案
#import "LGReportDetailMedicineCell.h"
#import "LGReportDetailContentCell.h"
#import "LGReportDetailFileCell.h"

#define viewHeight  (Screen_H-topBarHeight-viewPix(120))
@interface LGReportDetailHospitalController2 ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)LGReportDetailHeaderView *headerView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSString *filePath;
/**   */
@property (nonatomic , strong)NSArray *titleArray;

@end

@implementation LGReportDetailHospitalController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleArray = @[@"出院诊断",@"诊疗小结",@"介入治疗",@"辅助检查",@"照护计划任务",@"用药方案",@"报告原文件"];
    [self.view addSubview:self.tableView];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.filePath = LGNSString(dataDic[@"report_file"]);
    [self.tableView reloadData];
}

-(void)checkFileInformation:(UIButton *)sender{
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
    controller.navigationView.title = self.dataDic[@"report_name"];
    controller.urlStr = self.filePath;
    [self.navigationController pushViewController:controller animated:YES];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0 || section == 2){
        return 1;
    }else if(section == 1){
        //诊疗小结
        return [self.dataDic[@"diagnosis_summary"] count];
        
    }else if(section == 3){
        //辅助检查
        return [self.dataDic[@"assistant_examination"] count];
        
    }else if(section == 4){
        //照护计划
        return [self.dataDic[@"care_plan"] count];
        
    }else if(section == 5){
        //用药方案
        return [self.dataDic[@"medicines"] count];
        
    }else if(section == 6 && self.filePath.length > 1){
        //报告文件
        return 1;
        
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0 || indexPath.section == 2){
        LGReportDetailContentCell *contentCell = [tableView dequeueReusableCellWithIdentifier:@"contentCell" forIndexPath:indexPath];
        contentCell.selectionStyle = UITableViewCellSelectionStyleNone;
        contentCell.title = self.titleArray[indexPath.section];
        if(indexPath.section == 0){
            //出院诊断
            contentCell.content = LGNSString(self.dataDic[@"discharge_diagnosis"]);
        }else{
            //介入治疗
            contentCell.content = LGNSString(self.dataDic[@"interventional_therapy"]);
        }
        return contentCell;
    }else if(indexPath.section == 1){
        //诊疗小结
        LGReportDetailDiagnosisCell *diagnosisCell = [tableView dequeueReusableCellWithIdentifier:@"diagnosisCell" forIndexPath:indexPath];
        diagnosisCell.selectionStyle = UITableViewCellSelectionStyleNone;
        diagnosisCell.dataDic = self.dataDic[@"diagnosis_summary"][indexPath.row];
        return diagnosisCell;
        
    }else if(indexPath.section == 3){
        //辅助检查
        NSDictionary *tempDic = self.dataDic[@"assistant_examination"][indexPath.row];
        if([LGNSString(tempDic[@"type"]) isEqualToString:@"1"]){
            //描述
            LGReportDetailInspectDescCell *descCell = [tableView dequeueReusableCellWithIdentifier:@"descCell" forIndexPath:indexPath];
            descCell.selectionStyle = UITableViewCellSelectionStyleNone;
            descCell.dataDic = tempDic;
            return descCell;
            
        }else{
            //指标
            LGReportDetailInspectTargetCell *targetCell = [tableView dequeueReusableCellWithIdentifier:@"targetCell" forIndexPath:indexPath];
            targetCell.selectionStyle = UITableViewCellSelectionStyleNone;
            targetCell.dataDic = tempDic;
            return targetCell;
        }
        
    }else if(indexPath.section == 4){
        //照护计划
        LGReportDetailCarePlanCell *planCell = [tableView dequeueReusableCellWithIdentifier:@"planCell" forIndexPath:indexPath];
        planCell.selectionStyle = UITableViewCellSelectionStyleNone;
        planCell.dataDic = self.dataDic[@"care_plan"][indexPath.row];
        return planCell;
        
    }else if(indexPath.section == 5){
        //用药方案
        LGReportDetailMedicineCell *medicineCell = [tableView dequeueReusableCellWithIdentifier:@"medicineCell" forIndexPath:indexPath];
        medicineCell.selectionStyle = UITableViewCellSelectionStyleNone;
        medicineCell.dataDic = self.dataDic[@"medicines"][indexPath.row];
        return medicineCell;
        
    }else{
        //报告文件
        LGReportDetailFileCell *fileCell = [tableView dequeueReusableCellWithIdentifier:@"fileCell" forIndexPath:indexPath];
        fileCell.selectionStyle = UITableViewCellSelectionStyleNone;
        fileCell.title = [NSString stringWithFormat:@"%@.pdf",self.dataDic[@"report_name"]];
        [fileCell.checkBtn addTarget:self action:@selector(checkFileInformation:) forControlEvents:UIControlEventTouchUpInside];
        return fileCell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if((section == 1 && [self.dataDic[@"diagnosis_summary"] count]>0) || (section == 3 && [self.dataDic[@"assistant_examination"] count]>0) || (section == 4 && [self.dataDic[@"care_plan"] count]>0) || (section == 5 && [self.dataDic[@"medicines"] count]>0) || (section == 6 && self.filePath.length > 1)){
        return viewPix(50);
        
    }else{
        return 0.01;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if((section == 1 && [self.dataDic[@"diagnosis_summary"] count]>0) || (section == 3 && [self.dataDic[@"assistant_examination"] count]>0) || (section == 4 && [self.dataDic[@"care_plan"] count]>0) || (section == 5 && [self.dataDic[@"medicines"] count]>0) || (section == 6 && self.filePath.length > 1)){
        NSString *title = LGNSString(self.titleArray[section]);
        CGFloat width = (title.length > 5) ? 145:112;
        LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
        headView.backgroundColor = [UIColor clearColor];
        headView.topMargin = viewPix(8);
        UIButton *titleBtn = [UIButton buttonWithTitle:title titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#1F6CC6" imageName:@""];
        titleBtn.frame = CGRectMake(viewPix(16), viewPix(20), width, 27);
        [titleBtn setBackgroundImage:[UIImage imageNamed:@"就诊检查-蓝色"] forState:UIControlStateNormal];
        [headView addSubview:titleBtn];
        return headView;
        
    }else{
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.1)];
        return headView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if((section == 1 && [self.dataDic[@"diagnosis_summary"] count]>0) || (section == 3 && [self.dataDic[@"assistant_examination"] count]>0) || (section == 4 && [self.dataDic[@"care_plan"] count]>0) || (section == 5 && [self.dataDic[@"medicines"] count]>0) || (section == 6 && self.filePath.length > 1)){
        return viewPix(16);
    }else{
        return 0.01;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if((section == 1 && [self.dataDic[@"diagnosis_summary"] count]>0) || (section == 3 && [self.dataDic[@"assistant_examination"] count]>0) || (section == 4 && [self.dataDic[@"care_plan"] count]>0) || (section == 5 && [self.dataDic[@"medicines"] count]>0) || (section == 6 && self.filePath.length > 1)){
        //诊疗小结
        LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(16))];
        footView.backgroundColor = [UIColor clearColor];
        footView.bottomMargin = viewPix(8);
        return footView;
        
    }else{
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.1)];
        return footView;
    }
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGReportDetailDiagnosisCell class] forCellReuseIdentifier:@"diagnosisCell"];
        [_tableView registerClass:[LGReportDetailInspectDescCell class] forCellReuseIdentifier:@"descCell"];
        [_tableView registerClass:[LGReportDetailInspectTargetCell class] forCellReuseIdentifier:@"targetCell"];
        [_tableView registerClass:[LGReportDetailCarePlanCell class] forCellReuseIdentifier:@"planCell"];
        [_tableView registerClass:[LGReportDetailMedicineCell class] forCellReuseIdentifier:@"medicineCell"];
        [_tableView registerClass:[LGReportDetailContentCell class] forCellReuseIdentifier:@"contentCell"];
        [_tableView registerClass:[LGReportDetailFileCell class] forCellReuseIdentifier:@"fileCell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    }
    return _tableView;
}


@end
